package com.jyu.hakkatranslation.controller.common;

import cn.hutool.core.util.StrUtil;
import com.jyu.hakkatranslation.config.MinioConfig;
import com.jyu.hakkatranslation.utils.MinioUtils;
import io.minio.SnowballObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/** Minio测试
 * @author Akihi
 * @create 2022/11/05
 */
@Slf4j
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig minioConfig;
/*
    *//**
     * 文件上传
     *
     * @param file
     *//*
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            //原文件名
            String fileName = file.getOriginalFilename();
            log.info("fileName----》{}", fileName);
            //新文件名： 当前时间毫秒数 + 源文件后缀
            String folder = "/mp3/";
            String newFileName = folder + System.currentTimeMillis() + "." + StrUtil.subAfter(fileName, ".",true);
            log.info("newFileName----》{}", newFileName);
            //文件类型
            String contentType = file.getContentType();
            System.out.println("contentType: " + contentType);
            *//*文件类型判断*//*
            *//*String[] correctContentType = new String[2];
            correctContentType[0] = "jpeg";
            correctContentType[1] = "png";
            if (!StrUtil.containsAny(contentType, correctContentType)){
                log.error("上传失败，请选择正确的文件类型");
                return "上传失败，请选择正确的文件类型";
            }*//*
            minioUtils.uploadFile(minioConfig.getBucketName(), file, newFileName, contentType);
            return "上传成功";
        } catch (Exception e) {
            log.error("上传失败，服务繁忙");
            return "上传失败，服务繁忙";
        }
    }

    *//**
     * 多文件上传
     *
     *//*
    @PostMapping("/uploads")
    public String uploadObjects(MultipartFile file1, MultipartFile file2) {
        try {
            //文件名
            String fileName1 = file1.getOriginalFilename();
            String fileName2 = file2.getOriginalFilename();
            List<SnowballObject> objects = new ArrayList<>();
            objects.add(
                    new SnowballObject("1.jpg", file1.getInputStream(),file1.getSize(),null)
            );
            objects.add(
                    new SnowballObject("2.jpg", file2.getInputStream(),file2.getSize(),null)
            );
            minioUtils.uploadObjects(minioConfig.getBucketName(), objects);
            return "上传成功";
        } catch (Exception e) {
            log.error("上传失败，服务繁忙");
            return "上传失败，服务繁忙";
        }
    }

    *//**
     * 删除
     *
     * @param fileName
     *//*
    @DeleteMapping("/")
    public void delete(@RequestParam("fileName") String fileName) {
        minioUtils.removeFile(minioConfig.getBucketName(), fileName);
    }

    *//**
     * 获取文件信息
     *
     * @param fileName
     * @return
     *//*
    @GetMapping("/info")
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return minioUtils.getFileStatusInfo(minioConfig.getBucketName(), fileName);
    }

    *//**
     * 获取文件外链
     * @param expireTime 过期时间，单位秒
     * @param fileName
     * @return
     *//*
    @GetMapping("/url")
    public String getPresignedObjectUrl(String fileName, Integer expireTime) {
        return minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName, expireTime);
    }*/

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtils.getObject(minioConfig.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败");
        }
    }


}
